﻿var _lang_singer = "歌手";
var _lang_song = "曲名";
var _lang_album = "专辑";
var _lang_by = "编者";
function Lrc(){}

Lrc.prototype.Init = function(tt, player, playerType, objectName, timer)
{
	if (this.TimerID)
	{
		this.Stop();
		clearTimeout(this.hailang);
	}
	
	this.Player = player;
	this.PlayerType = playerType;
	this.ObjectName = objectName;
	this.Timer = timer;
	this.gsh = _lang_singer + ":~1~\n" + _lang_song + ":~2~\n" + _lang_album + ":~3~\n" + _lang_by + ":~4~";
	this.inr = [];        //行
	this.Offset = 0;        //余补时间
	this.hailang;
	this.dts = -1;        //当前行显示的s
	this.dte = -1;        //当前行显示的e
	this.dlt = -1;        //当前行
	this.ddh;        //当前行数据
	this.fjh;
	//以上几个属性是为了判断是否还在上次显示的时间范围，以减少循环次数
	
	if (tt == null)
	{
		this.print("");
		$("#lrcwt1").text("");
		$("#lrcwt2").text("");
		$("#lrcwt3").text("");
		$("#lrcwt4").text("");
		$("#lrcwt5").text("");
		$("#lrcbc").width(0);
		return;
	}
	
	//取offset余补时间
	if(/\[offset\:(\-?\d+)\]/i.test(tt))
		this.Offset = RegExp.$1/1000;
	
	//歌手，曲名，专辑，编者等信息
	this.gsh = this.gsh.replace("~1~",(/\[ar:([^\[\]:]+)\]/i.test(tt))?RegExp.$1:"----");
	this.gsh = this.gsh.replace("~2~",(/\[ti:([^\[\]:]+)\]/i.test(tt))?RegExp.$1:"----");
	this.gsh = this.gsh.replace("~3~",(/\[al:([^\[\]:]+)\]/i.test(tt))?RegExp.$1:"----");
	this.gsh = this.gsh.replace("~4~",(/\[by:([^\[\]:]+)\]/i.test(tt))?RegExp.$1:"----");
	$("lrcxx").text(this.gsh);
	
	//去掉注解
	tt = tt.replace(/\[\:\][^$\n]*(\n|$)/g,"$1");
	
	//去掉除时间标签的其它标签
	tt = tt.replace(/\[[^\[\]\:]*\]/g,"");
	tt = tt.replace(/\[[^\[\]]*[^\[\]\d]+[^\[\]]*\:[^\[\]]*\]/g,"");
	tt = tt.replace(/\[[^\[\]]*\:[^\[\]]*[^\[\]\d\.]+[^\[\]]*\]/g,"");
	tt = tt.replace(/<[^<>]*[^<>\d]+[^<>]*\:[^<>]*>/g,"");
	tt = tt.replace(/<[^<>]*\:[^<>]*[^<>\d\.]+[^<>]*>/g,"");
	
	
	while(/\[[^\[\]]+\:[^\[\]]+\]/.test(tt))
	{
		tt = tt.replace(/((\[[^\[\]]+\:[^\[\]]+\])+[^\[\r\n]*)[^\[]*/,"\n");
		var zzzt = RegExp.$1;
		/^(.+\])([^\]]*)$/.exec(zzzt);
		var ltxt = RegExp.$2;
		var eft = RegExp.$1.slice(1,-1).split("][");
		for(var ii=0; ii<eft.length; ii++)
		{
			var sf = eft[ii].split(":");
			var tse = parseInt(sf[0],10) * 60 + parseFloat(sf[1]);
			var sso = { t:[] , w:[] , n:ltxt }
			sso.t[0] = tse-this.Offset;
			this.inr[this.inr.length] = sso;
		}
	}
	this.inr = this.inr.sort( function(a,b){return a.t[0]-b.t[0];} );
	
	for(var ii=0; ii<this.inr.length; ii++)
	{
		while(/<[^<>]+\:[^<>]+>/.test(this.inr[ii].n))
		{
		  this.inr[ii].n = this.inr[ii].n.replace(/<(\d+)\:([\d\.]+)>/,"%=%");
		  var tse = parseInt(RegExp.$1,10) * 60 + parseFloat(RegExp.$2);
		  this.inr[ii].t[this.inr[ii].t.length] = tse-this.Offset;
		}
		$("#lrcbc").html("<font>"+ this.inr[ii].n.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/%=%/g,"</font><font>") +" </font>");
		var fall = $("#lrcbc font");
		for(var wi=0; wi<fall.length; wi++)
		  this.inr[ii].w[this.inr[ii].w.length] = fall.eq(wi).width();
		this.inr[ii].n = $("#lrcbc").text();
	}
	
	this.print("");
	$("#lrcwt1").text("");
	$("#lrcwt2").text("");
	$("#lrcwt3").text("");
	$("#lrcwt4").text("");
	$("#lrcwt5").text("");
	$("#lrcbc").width(0);
};

Lrc.prototype.Run = function()
{
	if (this.PlayerType == "mp")
		this.runing(this.Player.controls.currentPosition, this.Player.currentMedia.duration);
	else
		this.runing(this.Player.GetPosition()/1000, this.Player.GetLength()/1000);
	
    this.TimerID = setTimeout(this.ObjectName + ".Run()", this.Timer);
};

Lrc.prototype.Stop = function()
{
	if (this.TimerID)
		clearTimeout(this.TimerID);
};

Lrc.prototype.runing = function(tme, plen)
{
	if (tme < this.dts || tme >= this.dte)
	{
		var ii;
		for (ii = this.inr.length - 1; ii>=0 && this.inr[ii].t[0] > tme; ii--){}
		if(ii<0) return;
		this.ddh = this.inr[ii].t;
		this.fjh = this.inr[ii].w;
		this.dts = this.inr[ii].t[0];
		this.dte = (ii<this.inr.length-1)?this.inr[ii+1].t[0]:plen;
		
		$("#lrcwt1").text(this.retxt(ii-3));
		$("#lrcwt2").text(this.retxt(ii-2));
		$("#lrcwt3").text(this.retxt(ii-1));
		$("#lrcwt4").text(this.retxt(ii+1));
		$("#lrcwt5").text(this.retxt(ii+2));
		this.print(this.retxt(ii));
		if(this.dlt==ii-1)
		{
			clearTimeout(this.hailang);
			this.golrcoll(0);
		}
		this.dlt = ii;
	}
	var bbw = 0;
	var ki;
	for (ki=0; ki<this.ddh.length && this.ddh[ki]<=tme; ki++)

		bbw += this.fjh[ki];
	var kt = ki-1;
	var sc = ((ki<this.ddh.length)?this.ddh[ki]:this.dte) - this.ddh[kt];
	var tc = tme - this.ddh[kt];
	bbw -= this.fjh[kt] - tc / sc * this.fjh[kt];
	if (bbw > $("#lrcbox").width())
		bbw = $("#lrcbox").width();
	$("#lrcbc").width(Math.round(bbw));
};

Lrc.prototype.retxt = function(i)
{
	return (i<0 || i>=this.inr.length)?"":this.inr[i].n;
};

Lrc.prototype.print = function(txt)
{
	$("#lrcbox").text(txt);
	$("#lrcbc").text(txt);
};

Lrc.prototype.golrcoll = function(s)
{
  $("#lrcoll").css("top", 25-(s++)*5);
  if ($.brower == "msie")
  {
	  $("#lrcwt1").get(0).filters.alpha.opacity = 90-s*18;
	  $("#lrcwt5").get(0).filters.alpha.opacity = s*18+10;
  }
  else if ($.brower == "mozilla")
  {
	  $("#lrcwt1").css("-moz-opacity", 90-s*18);
	  $("#lrcwt5").css("-moz-opacity", s*18+10);
  }
  if(s<=5)
    this.hailang = setTimeout(this.ObjectName + ".golrcoll(" + s + ")", 120);
};
